package com.yuanduo_app.window;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.CheckBox;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;
import com.xujl.fastlib.base.BaseRvAdapter;
import com.xujl.fastlib.http.NetworkManager;
import com.xujl.fastlib.utils.SpHelper;
import com.xujl.fastlib.utils.StringUtils;

import com.yuanduo_app.R;
import com.yuanduo_app.bridge.RNBridgeModule;
import com.yuanduo_app.consts.RNEvent;
import com.yuanduo_app.consts.SpName;
import com.yuanduo_app.http.ApiService;
import com.yuanduo_app.manager.RNModuleManager;
import com.yuanduo_app.utils.ViewHelper;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 变更url弹窗
 */
public class ChangeUrlWindow extends BaseWindow implements View.OnClickListener, OnItemClickListener {
    private List<Bean> mList;
    private BaseRvAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private EditText mEditText;
    private CheckBox mCbEt;

    public ChangeUrlWindow (Context context) {
        super(context);
        setPopupGravity(Gravity.CENTER);
    }

    @Override
    public View onCreateContentView () {
        final View view = createPopupById(R.layout.window_debug_button);
        view.findViewById(R.id.tv_confirm).setOnClickListener(this);
        mList = new ArrayList<>();
        initList();
        mAdapter = new BaseRvAdapter<Bean>(R.layout.item_debug, mList) {
            @Override
            protected void convert (@NotNull BaseViewHolder helper, Bean item) {
                helper.setText(R.id.cb_url, item.getUrl())
                        .setText(R.id.tv_desc, item.getDesc());
                ((CheckBox) helper.getView(R.id.cb_url)).setChecked(item.isChecked);
            }
        };
        mAdapter.setOnItemClickListener(this);
        mRecyclerView = view.findViewById(R.id.rv);
        mEditText = view.findViewById(R.id.et);
        mCbEt = view.findViewById(R.id.cb_et);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
        return view;
    }

    private void initList () {
//        final String url = SpHelper.getInstance().getString(SpName.BASE_URL);
        final String url = NetworkManager.getInstance().getUrl();
        mList.add(new Bean(ApiService.NORMAL_URL, "正式环境", StringUtils.equals(url, ApiService.NORMAL_URL)));
        mList.add(new Bean(ApiService.TEST_URL, "测试环境", StringUtils.equals(url, ApiService.TEST_URL)));
        mList.add(new Bean(ApiService.DEV_URL, "DEV环境", StringUtils.equals(url, ApiService.DEV_URL)));
        mList.add(new Bean(ApiService.UAT_URL, "UAT环境", StringUtils.equals(url, ApiService.UAT_URL)));
    }

    @Override
    public void showPopupWindow () {
        super.showPopupWindow();
        if (mRecyclerView.getAdapter() == null) {
            mRecyclerView.setAdapter(mAdapter);
        }
    }

    @Override
    protected Animation onCreateShowAnimation () {
        return getScaleAnimation(0.3f, 1f, 0f, 1f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    }

    @Override
    protected Animation onCreateDismissAnimation () {
        return getScaleAnimation(1f, 0.3f, 1f, 0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    }

    @Override
    public void onClick (View view) {
        RNBridgeModule module = RNModuleManager.getInstance().getModule(RNBridgeModule.class);
        Map<String, String> map = new HashMap<>(1);
        final String text = ViewHelper.getText(mEditText);
        if (mCbEt.isChecked() && !StringUtils.isEmpty(text)) {
            SpHelper.getInstance().edit().putString(SpName.BASE_URL, text).commit();
            NetworkManager.getInstance().setDefaultUrl(text);
            if (module != null) {
                map.put("host", text);
                //通知RRN修改请求host
                module.sendEvent(RNEvent.CHANGE_HOST, new Gson().toJson(map));
            }
            dismiss();
            return;
        }
        for (Bean bean : mList) {
            if (bean.isChecked) {
                SpHelper.getInstance().edit().putString(SpName.BASE_URL, bean.getUrl()).commit();
                NetworkManager.getInstance().setDefaultUrl(bean.getUrl());
                if (module != null) {
                    map.put("host", bean.getUrl());
                    module.sendEvent(RNEvent.CHANGE_HOST, new Gson().toJson(map));
                }
                dismiss();
                return;
            }
        }
    }

    @Override
    public void onItemClick (BaseQuickAdapter adapter, View view, int position) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setChecked(i == position);
        }
        mAdapter.notifyDataSetChanged();
    }

    private class Bean {
        private String url;
        private String desc;
        private boolean isChecked;

        public Bean (String url, String desc, boolean isChecked) {
            this.url = url;
            this.desc = desc;
            this.isChecked = isChecked;
        }

        public String getDesc () {
            return desc;
        }

        public void setDesc (String desc) {
            this.desc = desc;
        }

        public String getUrl () {
            return url;
        }

        public void setUrl (String url) {
            this.url = url;
        }

        public boolean isChecked () {
            return isChecked;
        }

        public void setChecked (boolean checked) {
            isChecked = checked;
        }
    }
}
